/*!
    \copyright  Copyright (c) 2022 - 2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    gatt_server_battery
    \brief
*/

#include "gatt_server_battery_advertising.h"

#include "le_advertising_manager.h"

#include <gatt.h>
#include <panic.h>

#define SIZE_BATTERY_ADVERT                 4

static const uint8 gatt_battery_advert_data[SIZE_BATTERY_ADVERT] = { \
    SIZE_BATTERY_ADVERT - 1, \
    ble_ad_type_complete_uuid16, \
    GATT_SERVICE_UUID_BATTERY_SERVICE & 0xFF, \
    GATT_SERVICE_UUID_BATTERY_SERVICE >> 8 \
};

static le_adv_item_handle gatt_server_battery_registered_handle = NULL;
static le_adv_item_data_t gatt_battery_advert;

static bool gattBatteryServer_GetItemData(le_adv_item_data_t * data)
{
    PanicNull(data);
    *data = gatt_battery_advert;
    return TRUE;
}

static bool gattBatteryServer_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ .placement = le_adv_item_data_placement_advert,
#ifdef PREFER_LEGACY_ADVERTISING
                                    .type = le_adv_type_legacy_connectable_scannable,
#else
                                    .type = le_adv_type_extended_connectable,
#endif
                                    .data_size = gatt_battery_advert.size };
    return TRUE;
}

le_adv_item_callback_t gattBatteryServer_AdvertisingManagerCallback =
{
    .GetItemData = &gattBatteryServer_GetItemData,
    .GetItemInfo = &gattBatteryServer_GetItemInfo,
};

bool GattServerBattery_SetupLeAdvertisingData(void)
{
    gatt_battery_advert.size = SIZE_BATTERY_ADVERT;
    gatt_battery_advert.data = gatt_battery_advert_data;
    gatt_server_battery_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &gattBatteryServer_AdvertisingManagerCallback);
    return (gatt_server_battery_registered_handle ? TRUE : FALSE);
}

bool GattServerBattery_UpdateLeAdvertisingData(void)
{
    return (gatt_server_battery_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(gatt_server_battery_registered_handle) : FALSE);
}
